<?php 
namespace plugins\finance\model;

use think\Model;
use think\Db;
use plugins\finance\model\FinanceExtractModel;	//提现
use plugins\finance\model\FinanceRechargeModel;	//充值
use plugins\pay\lib\Pay;	//支付接口

class FinanceModel extends Model
{	

	//财务账户的创建
	//因不同的表字段不同所以无法检测
	public function createFinanceAccount($type,$id)
	{
		// $config = GetModuleConfig('finance','config','setting.json');
		// $config = json_decode($config,true);
        Db::startTrans();
        try{

        	$check = $this->get(['finance_type_id'=>$id,'finance_type'=>$type]);

        	if($check){
        		return ['status'=>false,'message'=>'已存在的账户'];
        	}

			$data['finance_id'] = uniqid('finance.');
			$data['finance_type'] = $type;
			$data['finance_type_id'] = $id;
			$result = $this->save($data);
			if($result){
				Db::commit();
				return ['status'=>true,'message'=>'添加成功'];
			}
            return ['status'=>false,'message'=>'添加失败'];
        }catch(\Exception $e){
            Db::rollback();
            return ['status'=>false,'message'=>$e->getMessage()];
        }		
	}

	//余额提现申请
	//绑定id 提现目标
	public function extract($bind_id,$money){
        Db::startTrans();
        try{
        	//绑定查询
        	$finance_bind_model = ( new FinanceBindModel )->get(['finance_id'=>$this->finance_id,'id'=>$bind_id]);
        	if($finance_bind_model == null){
        		return ['status'=>false,'message'=>'错误的绑定信息，请刷新重试'];
        	}


        	//资金判断
        	if($this->balance < $money){
        		return ['status'=>false,'message'=>'账户余额不足'];
        	}

			//资金冻结
        	$this->balance = $this->balance - $money;
        	$this->frozen_money = $this->frozen_money + $money;

			//生成提现记录
			$result = ( new FinanceExtractModel )->frozen($this->finance_id,$finance_bind_model->bind_aim,$finance_bind_model->openid,$money,$finance_bind_model->bind_name);
			if($result){
	        	$this->save();
	        	Db::commit();
	        	return ['status'=>true,'message'=>'申请成功'];			
			}

			return ['status'=>false,'message'=>'申请失败请重试'];

        }catch(\Exception $e){

        	// dump($e->getMessage());
        	return ['status'=>false,'message'=>$e->getMessage()];

        	Db::rollback();
        	return false;
        }
	}

	//充值
	//充值方式  充值金额
	public function recharge($pay_type,$money)
	{
		Db::startTrans();
		try{
			//生成充值记录
			$result = ( new FinanceRechargeModel )->recharge($this->finance_id,$pay_type,$money);

			if($result['status']){

				//支付宝
				if($pay_type == 'alipay'){
					$type = 'alipay';
	                $alipay = Pay::createInstance(  $type  );	//创建pay的class

	                $bizcontent = [];
	                $bizcontent['out_trade_no'] = $result['message'];

		            $bizcontent['total_amount'] = $money;
		            $bizcontent['subject'] = '平台余额充值:'.$money;
	                
	          		$bizcontent = json_encode($bizcontent);	

	          		$res = $alipay->expense( $bizcontent ) ;	
				}

				//微信
				if($pay_type == 'wechatpay'){
					
	                $type = 'wechatpay' ; //wechatpay为微信支付方式标识
	                $wechatpay= Pay::createInstance(  $type  );	//创建pay的class

	                $bizcontent = [];

	                $bizcontent['out_trade_no'] = $result['message'];

	                $bizcontent['body'] = '平台余额充值:'.$money;
	                $bizcontent['total_fee'] = $money * 100;
	                $bizcontent['trade_type'] = 'APP';	//交易类型

	          		$bizcontent = json_encode($bizcontent);	

	          		$res = $wechatpay->expense( $bizcontent ) ;				
				}

          		
				Db::commit();
				return ['status'=>true,'message'=>'请唤起充值','data'=>$res];
			}
			return ['status'=>false,'message'=>'充值失败请重试','data'=>null];

		}catch(\Exception $e){
        	dump($e->getMessage());
        	Db::rollback();
        	return false;			
		}
	}

	//绑定列表
	public function appBindList()
	{
		$list = ( new FinanceBindModel )
				->order('bind_aim')
				->where(['finance_id'=>$this->finance_id])
				->select();
		return $list;
	}

	//app与支付方式绑定
	public function appBind($pay_type,$bind){

		//查重
		$check = ( new FinanceBindModel )->get(['bind_aim'=>$pay_type,'finance_id'=>$this->finance_id,'openid'=>$bind['openId']]);
		if($check != null){
			return ['status'=>false,'message'=>'绑定失败,该账号已绑定'];
		}

		$finance_bind_model = ( new FinanceBindModel );

		$finance_bind_model->finance_id = $this->finance_id;

		$result = $finance_bind_model->appBind($pay_type,$bind);

		if($result['status']){
			return ['status'=>true,'message'=>'绑定成功'];
		}
		return ['status'=>false,'message'=>'绑定失败'];
	}

	//删除账户下绑定的账户id
	public function appBindDel($bindId){
		$check = ( new FinanceBindModel )->get(['finance_id'=>$this->finance_id,'id'=>$bindId]);
		if($check == null){
			return ['status'=>false,'message'=>'删除失败,该账户下无此记录'];
		}

		$result = $check->delete();

		if($result){
			return ['status'=>true,'message'=>'删除完成'];
		}

		return ['status'=>false,'message'=>'删除失败,请刷新页面重试'];

	}

}